package com.suke.czx.modules.wx.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suke.czx.common.utils.JsonResult;
import com.suke.czx.modules.wx.cons.Constant;
import com.suke.czx.modules.wxadmin.entity.DkBookEntity;
import com.suke.czx.modules.wxadmin.entity.DkBookExchangeEntity;
import com.suke.czx.modules.wxadmin.entity.DkUserAddressEntity;
import com.suke.czx.modules.wxadmin.entity.DkUserEntity;
import com.suke.czx.modules.wxadmin.service.DkBookExchangeService;
import com.suke.czx.modules.wxadmin.service.DkBookService;
import com.suke.czx.modules.wxadmin.service.DkUserAddressService;
import com.suke.czx.modules.wxadmin.service.DkUserService;

import me.chanjar.weixin.mp.bean.result.WxMpUser;

@RestController
@RequestMapping("/wx/book")
public class WxBookController extends BaseController{
	@Autowired
	private DkBookService dkBookService;
	
	@Autowired
    private DkUserService dkUserService;
	
	@Autowired
	private DkBookExchangeService dkBookExchangeService;
	
	@Autowired
	private DkUserAddressService dkUserAddressService;
	
	
	private org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
	
	
	@RequestMapping(value="/list")
	public String list(){
		List<DkBookEntity> lsit = dkBookService.queryList(new HashMap<String, Object>());
		return new JsonResult(200,"ok",lsit).toJson();
	}
	
	@RequestMapping(value="/exchange" ,produces = "application/json; charset=UTF-8")
	public String exchange(@RequestBody String requestBody){
		WxMpUser wxMpUser = (WxMpUser) getSession().getAttribute(Constant.WX_USER);
		if(null==wxMpUser){
			return new JsonResult(24,"用户未登录，请先登录！").toJson();
		}
		DkUserEntity dkUser = dkUserService.queryObject(wxMpUser.getOpenId(),false);
		if(dkUser.getState()==0){
			return new JsonResult(-1,"只有订购用户才能兑换图书！").toJson();
		}
		JSONArray array  = JSONObject.parseArray(requestBody);
		if(null==array || array.size()<=0){
			return new JsonResult(-1,"没有选购书籍").toJson();
		}
		DkUserAddressEntity dkUserAddress = dkUserAddressService.queryObject(wxMpUser.getOpenId());
		if (null == dkUserAddress) {
			return new JsonResult(201, "没有登记过地址信息，跳转登记界面").toJson();
		}
		
		
		DkUserEntity user = dkUserService.queryObject(wxMpUser.getOpenId(),false);
		int price = 0;
		List<DkBookEntity> book_list = new ArrayList<DkBookEntity>();
		for(int i=0;i<array.size();i++){
			DkBookEntity book = dkBookService.queryObject(array.getInteger(i));
			book_list.add(book);
			price = price + book.getPrice();
		}
		int coin_p = user.getCoin().setScale(0, BigDecimal.ROUND_DOWN).intValue();
		if(price>coin_p){
			return new JsonResult(-1,"您的领书币不足！").toJson();
		}
		//1、扣钱
		for(int i=0 ; i<price ; i++){
			int r = dkUserService.cutCoin(user.getOpenid());
		}
		logger.info("==============>兑换扣除书币用户ID：" + user.getOpenid() + " 扣除个数：" + price + "购买图书ID：" + array.toJSONString());
		
		//2、构建兑换记录
		for(DkBookEntity b : book_list){
			DkBookExchangeEntity be = new DkBookExchangeEntity();
			be.setBookId(b.getId());
			be.setBookName(b.getName());
			be.setCreatetime(new Date());
			be.setOpenid(user.getOpenid());
			be.setPrice(b.getPrice());
			be.setState(0);
			dkBookExchangeService.save(be);
		}
		
		dkUser = dkUserService.queryObject(wxMpUser.getOpenId(),false);
		
		
		return new JsonResult(200,"兑换成功！",dkUser).toJson();
	}
	
	@RequestMapping(value="/record/exchange")
	public String record_exchange(){
		WxMpUser wxMpUser = (WxMpUser) getSession().getAttribute(Constant.WX_USER);
		if(null==wxMpUser){
			return new JsonResult(24,"用户未登录，请先登录！").toJson();
		}
		List<DkBookExchangeEntity> list = dkBookExchangeService.queryListByOpenid(wxMpUser.getOpenId());
		
		return new JsonResult(200,"OK",list).toJson();
	}
	
	@RequestMapping(value="/record/inv")
	public String record_inv(){
		WxMpUser wxMpUser = (WxMpUser) getSession().getAttribute(Constant.WX_USER);
		if(null==wxMpUser){
			return new JsonResult(24,"用户未登录，请先登录！").toJson();
		}
		List<DkUserEntity> users = dkUserService.queryInvUser(wxMpUser.getOpenId());
		return new JsonResult(200,"Ok",users).toJson();
	}
	
	

}
